package com.caowei.weicimpress.weicimpress.widget;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by caoweiPc on 2014/7/22.
 */
public class LeftActionbarDropdownListWidget extends DropdownListWidget {
    private static final String TAG=LeftActionbarDropdownListWidget.class.getSimpleName();
    private List<Callback> callbacks=new ArrayList<Callback>();
    private int  performItemClickHashcode=-1;

    public LeftActionbarDropdownListWidget(Context context) {
        super(context);
    }
    public LeftActionbarDropdownListWidget(final Context context,
                              final AdapterView.OnItemClickListener onItemClickListener) {
        super(context,onItemClickListener);
    }

    @Override
    protected void prepareShow(View anchor) {
        super.prepareShow(anchor);
        for(Callback callback:callbacks){
            callback.callback();
            if(performItemClickHashcode!=-1&&performItemClickHashcode==callback.hashCode()){
                callbacks.remove(callback);
                performItemClickHashcode=-1;
            }
        }
    }

    public void performLastItemClick(){
        Callback callback= new Callback(){
            @Override
            public void callback() {
                mListView.setFinishChildrenListener(new OnListViewFinishChildren() {
                    @Override
                    public void onFinish() {
                        BaseAdapter adapter= (BaseAdapter) mListView.getAdapter();
                        int position=adapter.getCount()-1;
                        Log.d(TAG,mListView.getChildCount()+"");
                        long id= mListView.getAdapter().getItemId(position);

                        if(mListView.getChildCount()==adapter.getCount()){
                            View v= mListView.getChildAt(position);
                            mListView.performItemClick(v, position, id);
                        }
                        mListView.setFinishChildrenListener(null);
                    }
                });
            }
        };
        for(Callback callback1:callbacks){
            if(callback1.hashCode()==performItemClickHashcode){
                callbacks.remove(callback1);
            }
        }
        callbacks.add(callback);
        performItemClickHashcode=callback.hashCode();
    }
}
